import { useState, useEffect, useRef } from "react";
export function useCountDown(count = 10) {
  const [num, setNum] = useState(count);
  const timer = useRef(null);
  const [isDisabled, setIsDisabled] = useState(false);
  const fn = () => {
    if (timer.current !== null) return;
    setIsDisabled(true);
    timer.current = setInterval(() => {
      setNum((c) => c - 1);
    }, 1000);
  };

  useEffect(() => {
    if (num <= 0) {
      clearInterval(timer.current);
      setNum(count);
      timer.current = null;
      setIsDisabled(false);
    }
  }, [num, count]);
  return [fn, num, isDisabled];
}
